package elion.com.theElion.fragmentServer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import elion.com.elion.R;

import static elion.com.theElion.util.ClassAttrsName.getAttrsName;

/**
 * Created by kansas on 2017/7/13.
 */

public class Consult_Adapter extends BaseAdapter {

    private Context context;
    private List<Consult_Info> datas = new ArrayList<>();
    private List<Map<String, String>> list;
    private LayoutInflater layoutInflater;

    private String name[];
    private Object object;

    public Consult_Adapter(Context context, String[] name, Object object, List<Map<String, String>> list, List<Consult_Info> datas, LayoutInflater layoutInflater) {
        this.context = context;
        this.name = getAttrsName(object);
        this.object = object;
        this.list = list;
        this.datas = datas;
        this.layoutInflater = layoutInflater;
    }

    public void addDataToAdapter(Consult_Info e) {
        datas.add(e);
    }

    public Consult_Adapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null)
        {

            convertView = layoutInflater.inflate(R.layout.consult_item, null);
            viewHolder = new ViewHolder(convertView);
////
//            viewHolder.answer = (TextView)convertView.findViewById(R.id.answer);
//            viewHolder.content = (TextView)convertView.findViewById(R.id.content);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        String left = datas.get(position).getAnswer();
        String right = datas.get(position).getContent();


        if (left == null) {
            viewHolder.content.setText(right);
            viewHolder.right.setVisibility(View.VISIBLE);
            viewHolder.left.setVisibility(View.INVISIBLE);
        }


        if (right == null) {
            viewHolder.answer.setText(left);
            viewHolder.left.setVisibility(View.VISIBLE);
            viewHolder.right.setVisibility(View.INVISIBLE);
        }
//        for (int i = 0; i < name.length; i++) {
//            try {
//                if (name[i].equals("direction")) {
//                    viewHolder.content.setTextSize(20);
//                }
//                ReflectUtil.useSetMethod(viewHolder, name[i],
//                        Html.fromHtml(list.get(position).get(name[i])).toString());
//
//
//
//
//
//            } catch (NoSuchMethodException e) {
//                e.printStackTrace();
//            } catch (InvocationTargetException e) {
//                e.printStackTrace();
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            }


        return convertView;

    }

//    public View getView(int position, View convertView, ViewGroup parent) {
//        ViewHolder viewHolder;
//        if (convertView == null) {
//
//            LayoutInflater inflater = LayoutInflater.from(context);
//            convertView = inflater.inflate(R.layout.consult_item, null);
//            viewHolder = new ViewHolder(convertView);
//////
////            viewHolder.answer = (TextView)convertView.findViewById(R.id.answer);
////            viewHolder.content = (TextView)convertView.findViewById(R.id.content);
//            convertView.setTag(viewHolder);
//
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
//
//
//        String left = datas.get(position).getAnswer();
//        String right = datas.get(position).getContent();
//
//
//        if (left == null) {
//            viewHolder.content.setText(right);
//            viewHolder.right.setVisibility(View.VISIBLE);
//            viewHolder.left.setVisibility(View.INVISIBLE);
//        }
//
//
//        if (right == null) {
//            viewHolder.answer.setText(left);
//            viewHolder.left.setVisibility(View.VISIBLE);
//            viewHolder.right.setVisibility(View.INVISIBLE);
//        }
////        for (int i = 0; i < name.length; i++) {
////            try {
////                if (name[i].equals("direction")) {
////                    viewHolder.content.setTextSize(20);
////                }
////                ReflectUtil.useSetMethod(viewHolder, name[i],
////                        Html.fromHtml(list.get(position).get(name[i])).toString());
////
////
////
////
////
////            } catch (NoSuchMethodException e) {
////                e.printStackTrace();
////            } catch (InvocationTargetException e) {
////                e.printStackTrace();
////            } catch (IllegalAccessException e) {
////                e.printStackTrace();
////            }
//
//
//        return convertView;
//
//    }

    public static class ViewHolder {
        public View rootView;
        public TextView content;
        public LinearLayout left;
        public TextView answer;
        public LinearLayout right;


        public TextView getContent() {
            return content;
        }

        public void setContent(String content) {
//            RichText.from(content).into(this.content);
            this.content.setText(content);
        }

        public TextView getAnswer() {
            return answer;
        }

        public void setAnswer(String answer) {

            this.answer.setText(answer);
        }

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.content = (TextView) rootView.findViewById(R.id.content);
            this.left = (LinearLayout) rootView.findViewById(R.id.left);
            this.answer = (TextView) rootView.findViewById(R.id.answer);
            this.right = (LinearLayout) rootView.findViewById(R.id.right);
        }

    }
}
